import java.text.DecimalFormat;
import java.util.Timer;

//Autkionsobjekt
public class Auction {
	private int id=0;
	
	private int duration=0;
	private String description="";
	private User creator=null;
	private User hbitUser = new User("none");
	private double hbid=0.00;
	private String end;
	private AuctionManager man;
	private DecimalFormat f = new DecimalFormat("#0.00"); 
	private Timer timer;
	
	
	public Auction(int id,int duration, String description, User creator,String end,AuctionManager man){
		this.id = id;
		this.description=description;
		this.duration = duration;
		this.creator = creator;
		this.man = man;
		this.end = end;
		this.start();
	
	}
	
	//startet Countown
	public void start() {
		timer = new Timer();
		timer.schedule(new AuctCollectorTask(man,this), duration*1000);	
	
		
	}
	//Endzeit
	public String getEnd(){
		return end;
	}

	public String getDescription(){
		return description;
	}
	public User getCreator(){
		return this.creator;
	}
	public String getCreatorName(){
		return this.creator.getName();
	}
	//Hoechstes Gebot
	public double getHBit(){
		return this.hbid;
	}
	//Hoechstbietender Objekt
	public User getHBitUser(){
		return this.hbitUser;
	}
	//Hoechstbietender Name
	public String getHBitUserName(){
		return this.hbitUser.getName();
	}
	//Neues Gebot (falls Gebot zu niedrig return false else true
	public boolean bid(double bid, User hbitUser){
		if (bid>hbid){
			if (!this.hbitUser.getName().equals("none")){
				String message="!new-bid "+getDescription();
				if (this.hbitUser.isLoggedin()){
					//Sendet Notification an ueberbotenen User
					new Notification(this.hbitUser.getPort(),message,this.hbitUser.getIP());
				}
				else{
					//Falls User nicht eingelogged ist speichert Notification
					//im Notificationpool
					this.hbitUser.addNotifications(message);
				}
			}
			//Uberschreiben der HUser
			this.hbitUser= hbitUser;
			hbid= bid;
			return true;
		}
		//Falls Gebot zu niedrig
		return false;
	}
	public int getId() {
		return id;
	}
	//Fuer Server shutdown alle Auktionen abbrechen
	public void stopAuct(){
		timer.cancel();
		timer.purge();
	}
	//Notification ueber Ende der Auktion
	public void notifyUser(){
		String message="!auction-ended "+hbitUser.getName()+" " +f.format(getHBit())+" "+getDescription();
		if (!this.hbitUser.getName().equals("none")){
			//Notification an Gewinner
			if (this.hbitUser.isLoggedin()){
				new Notification(this.hbitUser.getPort(),message,this.hbitUser.getIP());
			}
			else{
				//Falls User nicht eingelogged ist speichert Notification
				//im Notificationpool
				this.hbitUser.addNotifications(message);
			}
		}
		//Notification an Creator
		if (this.creator.isLoggedin()){
			new Notification(this.creator.getPort(),message,this.creator.getIP());
		}
		else{
			//Falls User nicht eingelogged ist speichert Notification
			//im Notificationpool
			this.creator.addNotifications(message);
		}
	}
}
